﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using cocos2d;

namespace CampaignWP7
{
    public class BackGround:CCSprite
    {
        public CCSprite CurrentSprite;
        public CCSprite DestSprite;
        public int CurrentBG_TextureIndex;
//         float width;
//         float height;
        //----static 
        //the format like BGTexture_1 ..and so on
        public const int BGTextureNum = 2;
        private static string[] m_BGTextureName = 
        {
            @"images/BGTexture_0",@"images/BGTexture_1"                                                
        };
        public BackGround(int BG_index)
        {
            initWithFile(ResDef.g_BlankTextureFile);
            CurrentBG_TextureIndex = BG_index; 
        }
        //
        public override bool init()
        {
            if (!base.init()) { return false; }
            //
            CurrentSprite = new CCSprite();
            DestSprite = new CCSprite();
            //
            string initName = m_BGTextureName[CurrentBG_TextureIndex];
            CurrentSprite.initWithFile(initName);
            DestSprite.initWithFile(ResDef.g_BlankTextureFile);
            
            //
            CurrentSprite.visible = true;
            DestSprite.visible = false;
            DestSprite.Opacity = 0;
            //             CurrentSprite.anchorPoint = new CCPoint(0.5f, 0.5f);
            //             DestSprite.anchorPoint = new CCPoint(0.5f, 0.5f);
            //             this.anchorPoint = new CCPoint(0.5f, 0.5f);

            CurrentSprite.position = new CCPoint(0, 0);
            DestSprite.position = new CCPoint(0, 0);


            //
            this.addChild(CurrentSprite, 1);
            this.addChild(DestSprite, 1);
            //
            return true;
           
        }
        //
        public void PlayChangeBG(int BG_index)
        {
            if (BG_index >= 0 && BG_index < BGTextureNum &&
                CurrentBG_TextureIndex != BG_index)
            {
                CurrentSprite.stopAllActions();
                DestSprite.stopAllActions();

                //
                bool ret = DestSprite.initWithFile(m_BGTextureName[BG_index]);
                DestSprite.visible = true;
                DestSprite.Opacity = 0;

                              
                CCFadeIn fadeIn = CCFadeIn.actionWithDuration(1.0f);
                CCFadeOut fadeOut = CCFadeOut.actionWithDuration(1.0f);
                CCFiniteTimeAction changeBG = CCCallFuncND.actionWithTarget(this,
                                               new SEL_CallFuncND(callbackChangeBG), this);
                CCAction change_fadeIn = CCSequence.actionOneTwo(fadeIn, changeBG);

                CurrentSprite.runAction(fadeOut);
                DestSprite.runAction(change_fadeIn);
                //
                CurrentBG_TextureIndex = BG_index;

            }
        }
        private void callbackChangeBG(CCNode sender, object data)
        {
            CurrentSprite = DestSprite;
            
        }
    }
}
